package com.crm.sys.tab;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.crm.sys.model.Configure;
import com.crm.sys.model.ConfigureItem;
import com.crm.sys.service.SysService;
/**
 *  配置表标签
 * @author Administrator
 *
 */
@Repository
public class ConfigureTag extends RequestContextAwareTag {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3258620188746752599L;
    
	private String name;  //配置项名称
    private String value; //传入的默认值
    private int type;  //类型  1 显示内容  2显示下拉框  3 radio
	private Integer minLength;
	private Integer maxLength;
	private String inputName;//文本框名称
	private String ext="..."; //未完全展示内容符号
	
 
	public void setInputName(String inputName) {
		this.inputName = inputName;
	}

	public void setMinLength(Integer minLength) {
		this.minLength = minLength;
	}

	public void setMaxLength(Integer maxLength) {
		this.maxLength = maxLength;
	}

	public void setExt(String ext) {
		this.ext = ext;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setName(String name) {
		this.name = name;
	}
	public void setType(int type) {
		this.type = type;
	}

	public ConfigureTag(){  
    }  
      
	@Override
	protected int doStartTagInternal() throws Exception {
        JspWriter out = this.pageContext.getOut();  
          
        try {  
        	SysService sysService  = (SysService)this.getRequestContext().getWebApplicationContext().getBean("sysService");  
         
        	  try{
        		  String outString ="";
        		 // System.out.println( pageContext.getSession().getAttribute("user"));
        		
        		    switch (type) {
					case 1:
						if(name.equals("build_category")){
							ConfigureItem   cf2 = sysService.getConfigureItemByValue(value.substring(0,2)+"000",name);
							String showname = cf2.getName()+"-";
							ConfigureItem   cf1 = sysService.getConfigureItemByValue(value,name);
							showname  += cf1.getName();
						    outString =showname;
						}else{
							ConfigureItem   cf1 = sysService.getConfigureItemByValue(value,name);
						    outString =cf1.getName();
						}
					
						break; 
					case 2:
						Configure   cf = sysService.getConfigureByName(name);
						outString = getSelectHtml(cf.getItem(),value);
						break; 	 
					case 3:
						Configure   cfone = sysService.getConfigureByName(name);
						outString = getRadioHtml(cfone.getItem(),value,inputName);
						break; 	   
					}
        		    
        		    try{
	        		    if(StringUtils.isNotEmpty(outString)){
	        		    	if(maxLength!=null&&outString.length()>maxLength){
	        		    		out.print(outString.substring(0,maxLength)+ext);
	        		    	}else{
	        		    		out.print(outString);
	        		    	}
	        		    }else{
	        		    	out.print(outString);
	        		    }
			       	}catch(Exception e1){
			       	 out.print("");  
			       	} 
		       	}catch(Exception e1){
		       	 out.print("");
		       	}
           
        } catch (IOException e) {  
            //e.printStackTrace();  
        }  
          
      
        return EVAL_BODY_INCLUDE;  
    }  
	/**
	 * 
	 * @param list
	 * @param value   默认值
	 * @return
	 * @throws Exception
	 */
	private String getSelectHtml(List<ConfigureItem> list,String value) throws Exception{
		  StringBuffer  s = new StringBuffer();
		  for(ConfigureItem item:list){
			  s.append("<option value='").append(item.getValue()).append("' ").append(item.getValue().equals(value)?"selected >":">").append(item.getName()).append("</option>");
		  }
		 return s.toString();
	}
	
	/**
	 * 
	 * @param list
	 * @param value   默认值
	 * @return
	 * @throws Exception
	 */
	private String getRadioHtml(List<ConfigureItem> list,String value,String name) throws Exception{
		  StringBuffer  s = new StringBuffer();
		  for(ConfigureItem item:list){
			  s.append("<input type=\"radio\" name=\""+name+"\"  value='").append(item.getValue()).append("' ").append(item.getValue().equals(value)?"checked=''":"''").append(" title='").append(item.getName()).append("'  />");
		  }
		 return s.toString();
	}
	
 
    public int doAfterBody() throws JspException{
       // System.out.println("处理标签体（after body）....");  
        return SKIP_BODY;  
    }  
    public int doEndTag() throws JspException{  
      //  System.out.println("对象正在处理结束标记.....");  
        return EVAL_PAGE;  
    }  
}
